﻿using System;
using System.Collections.Generic;
using System.Web;
using App.Utilities.Web.Handlers;
using System.Data;
using Newtonsoft.Json;
using System.Configuration;
using CRMBiz;
using CRMWeb.Utilities.Context;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for SMSAccountSetting
    /// </summary>
    public class SMSAccountSetting : BaseHandler
    {
        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();
            //e.Cancel = true;
            SetResponseContentType(ResponseContentTypes.HTML);
        }

        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        //Bind Sms Accounts
        public void BindSmsAccounts()
        {
            try
            {
                string strJson = JsonConvert.SerializeObject(SmsSettingHandler.BindSMSAccounts());
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        //Get Sms account data
        public void GetSmsAccountData()
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = SmsSettingHandler.GetSMSAccountData(pageIndex, pageSize);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";

                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        //Add new setting
        public int AddSettings(string Company, string SmsAccount, int IsDefault)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = SmsSettingHandler.AddSettings(Company, SmsAccount, IsDefault);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }


        //Edit Settings
        public int EditSettings(int ID, string Company, string SmsAccount, int IsDefault)
        {
            int intReturnValue = 0;
            try
            {
                intReturnValue = SmsSettingHandler.EditSettings(ID, Company, SmsAccount, IsDefault);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return intReturnValue;
        }

        //Delete setting:
        public bool DeleteSettings(int ID)
        {
            bool flag = false;
            try
            {
                flag = SmsSettingHandler.DeleteSettings(ID);
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return flag;
        }

        public void SelectFromSMSID(string Company)
        {
            DataTable dt = null;
            try
            {
                dt = SmsSettingHandler.BindFromSMSId(Company);
                if (dt.Rows.Count > 0)
                    context.Response.Write(JsonConvert.SerializeObject(dt));
                else
                    context.Response.Write("0");
            }
            catch (Exception ex)
            {
                context.Response.Write("Error");
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

    }
}